package com.SpringRSSReader;
//import java.util.SimpleDateFormat;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.util.Log;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

//import android.util.Log;

//import android.sax.Element;

public class RssItem {
	private String title;
	private String description;
	private Date pubDate;
	private String link;
	private String enclosure;
	
	public RssItem(String title, String description, Date pubDate, String link, String enclosure) {
		this.title = title;
		this.description = description;
		this.pubDate = pubDate;
		this.link = link;
		this.enclosure = enclosure;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getPubDate() {
		return pubDate;
	}

	public void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
	public String getEnclosure() {
		return enclosure;
	}

	public void setEnclosure(String enclosure) {
		this.enclosure = enclosure;
	}
	
	@Override
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");
		String result = title + "   ( " + sdf.format(this.pubDate) + " )";
		return result;
	}
	public static ArrayList<RssItem> getRssItems(String feedUrl) {
		
		ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
		
		try {
			//open an URL connection make GET to the server and
			//take xml RSS data
			URL url = new URL(feedUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream is = conn.getInputStream();
		
				//DocumentBuilderFactory, DocumentBuilder are used for
				//xml parsing
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
		
				//using db (Document Builder) parse xml data and assign
				//it to Element
				Document document = db.parse(is);
				Element element = document.getDocumentElement();
		
				//take rss nodes to NodeList
				NodeList nodeList = element.getElementsByTagName("item");
		
				if (nodeList.getLength() > 0) {
					Log.i("SPRING_TAG","Node Counts:"+ nodeList.getLength());
					for (int i = 0; i < nodeList.getLength(); i++) {
		
						//take each entry (corresponds to <item></item> tags in
						//xml data
			
						Element entry = (Element) nodeList.item(i);
			
						Element _titleE = (Element) entry.getElementsByTagName("title").item(0);
						Element _descriptionE = (Element) entry.getElementsByTagName("description").item(0);
						Element _pubDateE = (Element) entry.getElementsByTagName("pubDate").item(0);
						Element _linkE = (Element) entry.getElementsByTagName("link").item(0);
						Element _enclosureE = (Element) entry.getElementsByTagName("enclosure").item(0);
						
						String _title = "";
						Date _pubDate = null;
						String _link = "";
						String _description = "";
						String _enclosure = "";
						//NodeList descNS = _descriptionE.getChildNodes();
						Node iterDesc = _descriptionE.getFirstChild();
						
						String tmpDesc;
						while (iterDesc!= null) {
							tmpDesc = iterDesc.getNodeValue();
							//Log.i("SPRING_TAG",tmpDesc);
							if (tmpDesc.startsWith("object") == true) {
								_description = _description.replaceAll("<$", "");
								break;
							} else {
								_description += tmpDesc;
							}
							iterDesc = iterDesc.getNextSibling();
						}
						//int j = 0;
						
						/*
						for (int j = 0; j < descNS.getLength(); j++){
							tmpDesc = descNS.item(j).getNodeValue();
							//Log.i("SPRING_TAG",tmpDesc);
							if (tmpDesc.startsWith("object") == true) {
								_description = _description.replaceAll("<$", "");
								break;
							} else {
								_description += tmpDesc;
							}
						}
						*/
						_description = _description.replaceAll("<br>", "\n");
						
						if (_titleE != null)
							_title = _titleE.getFirstChild().getNodeValue();
						
						if (_pubDateE != null)
							_pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
						
						if (_linkE != null)
							_link = _linkE.getFirstChild().getNodeValue();
							
						if (_enclosureE != null)
							_enclosure = _enclosureE.getAttribute("url");
						
						//create RssItemObject and add it to the ArrayList
						RssItem rssItem = new RssItem(_title, _description, _pubDate, _link, _enclosure);
						rssItems.add(rssItem);
					} // for i
				} // if nodeList

			} // if OK
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return rssItems;
	} // getRssItems
}
